package com.littleHan.brushQuestions.web.start.service.impl;

import com.alibaba.fastjson.JSON;
import com.littleHan.brushQuestions.common.Result;
import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.utils.JwtUtil;
import com.littleHan.brushQuestions.web.frontPage.dao.FrontPageDao;
import com.littleHan.brushQuestions.web.start.dao.LastIndexDao;
import com.littleHan.brushQuestions.web.start.pojo.GeneralUser;
import com.littleHan.brushQuestions.web.start.pojo.vo.TopicVo;
import com.littleHan.brushQuestions.web.start.service.LastIndexService;
import com.littleHan.brushQuestions.web.userCenter.pojo.Announcement;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class LastIndexServiceImpl implements LastIndexService {

    @Autowired
    private LastIndexDao lastIndexDao;

    @Autowired
    private FrontPageDao frontPageDao;


    @Override
    public String testIsLogin(HttpServletRequest request) {
        String jwt = request.getHeader("Authorization");
        Claims claims = null;
        int isLogin = 1;
        if (jwt != null && !"null".equals(jwt)) {
            try {
                //解密去验证jwt
                claims = JwtUtil.parseJWT(jwt);
            } catch (RuntimeException e) {
                //抛异常说明jwt不合法，疑似被更改
                System.out.println("被拦截");
                //被拦截打回403状态码告诉前端需要登录
                isLogin = 0;
            }
        }
        if (isLogin == 1) {
            return JSON.toJSONString(new Result().setCode(201).setMessage("已经登录过"));
        }
        return null;
    }

    @Override
    public GeneralUser getWebAdminInfo() throws FailedToLoadException {
        GeneralUser generalUser = lastIndexDao.getWebAdminInfo();
        if (generalUser == null) {
            throw new FailedToLoadException("获取站长信息失败");
        }
        return generalUser;
    }

    /*@Override
    public GeneralUser getWebAdminInfo() throws FailedToLoadException {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(GeneralUser.class));
        //先从redis中获取有没有
        GeneralUser generalUser = (GeneralUser) redisTemplate.opsForValue().get("webAdminInfo");
        if (generalUser == null) {
            synchronized (this) {
                if (generalUser == null) {
                    //从数据库中获取
                    generalUser = lastIndexDao.getWebAdminInfo();
                    if(generalUser==null){
                        throw new FailedToLoadException("获取站长信息失败");
                    }
                    //缓存到redis中
                    redisTemplate.opsForValue().set("webAdminInfo", generalUser);
                    return generalUser;
                }
            }
        }

        return generalUser;
    }*/

    @Override
    public List<TopicVo> getTopic() throws FailedToLoadException {
        List<TopicVo> topicList = lastIndexDao.getFiveTopic();
        if (topicList.isEmpty()) {
            throw new FailedToLoadException("获取题目失败");
        }
        for (TopicVo t : topicList) {
            //获取收藏数量
            int collectNum = lastIndexDao.getCollectNum(t.getId());
            //获取评论数量
            int commentNum = lastIndexDao.getCommentNum(t.getId());
            t.setCollectNum(collectNum);
            t.setCommentNum(commentNum);
        }
        return topicList;
    }

    @Override
    public List<Announcement> getAnnouncementList() {
        List<Announcement> list = frontPageDao.getAnnouncementList();
        return list;
    }

    @Override
    public List<GeneralUser> getCommentLikeMaxList() {
        List<GeneralUser> list = lastIndexDao.getCommentLikeMaxList();
        return list;
    }
}
